<?php
/**
 * 后台管理员操作
 */
namespace app\modules\controllers;
use yii\web\Controller;
use app\modules\models\Admin;
use Yii;
use yii\data\Pagination;
class ManageController extends Controller{

	//public $layout = false;
/**
 * 忘记密码，通过发送邮件更改密码
 */
	public function actionMailchangepass(){
		$this->layout = false;
		$time = Yii::$app->request->get("timestamp");
		$adminuser = Yii::$app->request->get('adminuser');
		$token  = Yii::$app->request->get('token');
		$model = new Admin;
		$myToken = $model->createToken($adminuser,$time);
		if($token != $myToken){
			$this->redirect(['public/login']);
			Yii:$app->end();
		}
		if(time() - $time >300){
			$this->redirect(['public/login']);
			Yii::$app->end();
		}
		if(Yii::$app->request->isPost){
			$post = Yii::$app->request->post();
			if($model->changePass($post)){
				Yii::$app->session->setFlash('info','密码修改成功');
			}
		}
		$model->adminuser = $adminuser;
		return $this->render("mailchangepass",['model'=>$model]);

	}
/**
 * 添加新管理员操作
 */
	public function actionReg(){
		$this->layout = 'layout1';
		$model = new Admin;
		if(Yii::$app->request->isPost){
			$post = Yii::$app->request->post();
			if($model->addManager($post)){
				Yii::$app->session->setFlash('info','管理员添加成功');
			}else{
				Yii::$app->session->setFlash('info','管理员添加失败');
			}
		}
		$model->adminpass = '';
		$model->repass = '';
		return $this->render('reg',['model'=>$model]);
	}
/**
 * 管理员列表
 */
	public function actionManagers(){
		$this->layout = 'layout1';
		$pagesize = Yii::$app->params['pageSize']['manage'];
		$query = Admin::find();
		$count = $query->count();
		$pages = new Pagination(['totalCount'=>$count,'defaultPageSize'=>$pagesize]);
		$model = $query->offset($pages->offset)->limit($pages->limit)->all();
		return $this->render('managers',['model'=>$model,'pages'=>$pages]);
	}

/**
 * 删除管理员
 * @author   zhuojundu
 */
	public function actionDel(){
		$id = (int)Yii::$app->request->get('adminid');
		if(empty($id)){
			$this->redirect(['manage/managers']);
			Yii::$app->end();
		}
		$model = new Admin;
		if($model->deleteAll('adminid = :id',[':id'=>$id])){
			Yii::$app->session->setFlash('info','删除成功');
			$this->redirect(['manage/managers']);
		}else{
			Yii::$app->session->setFlash('info','删除失败');
			$this->redirect(['manage/managers']);
		}
	}

/**
 * 修改密码
 * @author   zhuojundu
 */
 	public function actionChangepass(){
 		$this->layout = 'layout1';
 		$model = Admin::find()->where('adminuser =:user',[':user'=>Yii::$app->session['admin']['adminuser']])->one();

 		if(Yii::$app->request->isPost){
 			$post = Yii::$app->request->post();//获取数据        
 			if($model->changePass($post)){
 				Yii::$app->session->setFlash('info','密码修改成功');
 			}else{
 				Yii::$app->session->setFlash('info','密码修改失败');
 			}
 		}
 		$model->adminpass ='';
 		$model->repass='';
 		return $this->render('changepass',['model'=>$model]);
 	}

 /**
  * 修改邮箱
  * @author   zhuojundu
  */	
 	public function actionChangeemail(){
 		$this->layout = 'layout1';
 		$model = Admin::find()->where('adminuser = :user',[':user'=>Yii::$app->session['admin']['adminuser']])->one();
 		if(Yii::$app->request->isPost){
 			$post = Yii::$app->request->post();
 			if($model->changeEmail($post)){
 				Yii::$app->session->setFlash('info','邮箱修改成功');
 			}else{
 				Yii::$app->session->setFlash('info','邮箱修改失败');
 			}
 		}
 		$model->adminpass = '';
 		return  $this->render('changeemail',['model'=>$model]);
 	}

}